//
//  WGComplicationwController.m
//  QuickCure1
//
//  Created by Apple on 16/11/3.
//  Copyright © 2016年 maoge. All rights reserved.
//

#import "WGComplicationwController.h"
#import "WGComplicationModel.h"
#import "WGComplicationCell.h"
#import <MJRefresh.h>


//注册cell
 static NSString *cellID = @"cell";
 static NSString *complicationCellID = @"complicationCell";

@interface WGComplicationwController ()

/**并发症数据*/
@property(nonatomic,strong)NSArray<WGComplicationModel *> *complicationList;

/**统计点击的个数*/
@property(nonatomic,assign)NSInteger selCount;

/*保存cell上的文字*/

@property(nonatomic,copy)NSString *selString;

/** 判断是否选择*/

@property(nonatomic,assign)BOOL isSelCell;

/**记录当前的indexpath*/

@property(nonatomic,strong)NSIndexPath *currentIndexPath;


@end

@implementation WGComplicationwController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"并发症";
    
    self.complicationList = [NSArray array];

    //创建可变数组
    self.selString = [NSString string];
    
    [self loadData];
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
         [self loadData];
        
    }];
    
    
    //搭建UI界面
    [self setupUI];
  
 
}

#pragma
#pragma mark - 加载数据
-(void)loadData{
   
    [[NetWorkManager sharedManager] loadComlicationDataWithPage:1 pageSize:15 ci2_id:3 completed:^(NSArray<WGComplicationModel *> *array, NSError *error) {
        
        self.complicationList = array;
        
        if (self.complicationList) {
            
        
            NSLog(@"在刷新");
            
            [self.tableView reloadData];
            
            [self.tableView.mj_header endRefreshing];
        }
        
    }];

}


#pragma
#pragma mark -  搭建UI界面
-(void)setupUI{
    
    //创建item
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithTitle:@"保存" style: UIBarButtonItemStyleDone target:self action:@selector(clickOk:)];
    
    self.navigationItem.rightBarButtonItem = rightItem;
    
    //注册cell
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:cellID];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"WGComplicationCell" bundle:nil] forCellReuseIdentifier:complicationCellID];

}


#pragma
#pragma mark - 实现代理方法

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.complicationList.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    WGComplicationCell *cell = [tableView dequeueReusableCellWithIdentifier:complicationCellID forIndexPath:indexPath];
    
    
    //取出数据
    WGComplicationModel *model = self.complicationList[indexPath.row];
    
    cell.model = model;
    
    return cell;
    
}

#pragma
#pragma mark - 选择cell
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
#warning 解决bug 当cell不同的数据加载

    if (_currentIndexPath == indexPath) {
        
        //取出选中的当前cell
        
        WGComplicationCell *cell = [tableView cellForRowAtIndexPath:_currentIndexPath];
        
        [cell setHighlighted: !self.isSelCell animated:YES];
        
#warning 必须reload方能解决bug
        [tableView reloadData];
        
    }else{
        WGComplicationCell *cell = [tableView cellForRowAtIndexPath:indexPath];

        [cell setHighlighted: YES animated:YES];
        
        self.selCount++;
        
        //获取当前的model
        WGComplicationModel *model = self.complicationList[indexPath.row];
       
        self.selString = [self.selString stringByAppendingString:[NSString stringWithFormat:@"%@   ",model.complication_name]];
        }
    
    NSLog(@"self.selString:%@",self.selString);
      _currentIndexPath = indexPath;
   
}

#pragma
#pragma mark - 点击确认按钮
-(void)clickOk:(UIBarButtonItem *)item{

    if (_ClickOKButton) {
        
        _ClickOKButton(self.selString,self.selCount);
    }
    
    [self.navigationController popViewControllerAnimated:YES];
    
}

-(void)viewWillAppear:(BOOL)animated{
    
    [self loadData];
}


@end
